<?php
/**
 * 获取用户权限
 * 用户权限 = 私有权限+角色权限
 * User: Administrator
 * Date: 2018\9\12 0012
 * Time: 15:51
 */

namespace app\Lib\Model\AdminAuth\GetAuthorization;


use app\Lib\InterfaceIn\AdminAuth\GetAuthorization\GetAuthorizationHandle;
use app\Lib\InterfaceIn\AdminAuth\MenuAuthHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\Role\Role;

class GetAuthorization implements GetAuthorizationHandle
{
    /** @var UserHandle $user */
    private $user;
    private $cache;
    private $userAuthClass;
    private $roleAuthClass;
    public function __construct(UserHandle $user, $cache =true)
    {
        $this->user  = $user;
        $this->cache = $cache;
    }

    private function getUserAuthClass(){
        if(empty($this->userAuthClass)){
            $this->userAuthClass = new GetUserAuth($this->user);
        }
        return $this->userAuthClass;
    }

    private function getRoleAuthClass(){
        if(empty($this->roleAuthClass)){
            /*TODO */
            //$this->roleAuthClass = new GetRoleAuth($this->user->getRoleClass());
            $this->roleAuthClass = new GetRoleAuth($this->user->getRoleClass());
        }
        return $this->roleAuthClass;
    }

    public function run()
    {
        $dataAuth = [];
        $userAuth = $this->getUserAuthClass()->run();
        $roleAuth = $this->getRoleAuthClass()->run();
        if(!empty($roleAuth)){
            /** @var MenuAuthHandle $v */
            foreach ($roleAuth as $v){
                $dataAuth[$v->getMenuId()] = $v;
            }
        }
        if(!empty($userAuth)){
            foreach ($userAuth as $v){
                $dataAuth[$v->getMenuId()] = $v;
            }
        }
        return $dataAuth;

    }

    /**
     * 删除缓存
     * @return bool
     */
    public function delCaChe()
    {
        $this->getRoleAuthClass()->delCache();
        $this->getUserAuthClass()->delCache();
        return true;
    }


}